package com.ycb.bookserver.service.impl;

import com.book.constant.MessageConstant;
import com.book.context.BaseContext;
import com.book.exception.ShoppingCartException;
import com.ycb.book.dto.ShoppingCartDTO;
import com.ycb.book.entity.ShoppingCart;
import com.ycb.bookserver.mapper.ShoppingCartMapper;
import com.ycb.bookserver.service.BookService;
import com.ycb.bookserver.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断购物车中是否存在此次添加商品
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId= BaseContext.getCurrentId();
        shoppingCart.setId(userId);
        List<ShoppingCart> shoppingCarts=shoppingCartMapper.getPartShoppingCart(shoppingCart);
       
        if(shoppingCarts==null){
            //不存在，添加
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUpdateTime(LocalDateTime.now());
            shoppingCartMapper.add(shoppingCart);

       }else {
            //存在，不添加，抛异常
            throw new ShoppingCartException(MessageConstant.COMMODITY_REPETITIVE);
        }

    }
}
